package JDBC.model;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlFunction;
import org.springframework.jdbc.object.SqlUpdate;

import javax.sql.DataSource;
import java.sql.Types;

/**
 * @Fou Jack of all trades and master of none.
 * @date 2022/1/6 16:19
 */
public class UserSqlUpdate extends SqlUpdate {


    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"SqlUpdate.xml"});
        BeanFactory factory = context;
        UserSqlUpdate userSqlUpdate = (UserSqlUpdate) factory.getBean("userSqlUpdate");
        userSqlUpdate.updateUserMoney("小李",66.06);

    }

    public UserSqlUpdate(DataSource ds) {
        setDataSource(ds);
        setSql("update t_user set money=? where name=?");
        declareParameter(new SqlParameter(Types.DOUBLE));
        declareParameter(new SqlParameter(Types.VARCHAR));
        compile();
    }

    private boolean updateUserMoney(String name, double money) {
        int row = update(new Object[] {new Double(money),new String(name)});
        if (row>0){
            System.out.println("数据修改成功！");
            return true;
        }
        return false;
    }

}
